package com.jyw.common.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * w_q
 *
 * @author
 * @date 2022/5/61:40
 * @Description:
 */
@Component
public class ExportUtils {


    private static final SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd");


    public static void exportExport(List<Map<String, String>> mapList, String template, String fileName, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mapList", mapList);
        export(map, template, fileName, response);
    }

    public static void exportExportTotal(List<Map<String, String>> mapList, List<Map<String, String>> totalColumn, String template, String fileName, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mapList", mapList);
        map.put("totalColumn", totalColumn);
        export(map, template, fileName, response);
    }

    /**
     * 动态生成其他参数
     *
     * @param mapList
     * @param map
     * @param template
     * @param fileName
     * @param response
     * @throws Exception
     */
    public static void exportExportTrendsTotal(List<Map<String, String>> mapList, Map<String, Object> map, String template, String fileName, HttpServletResponse response) throws Exception {
        map.put("mapList", mapList);
        export(map, template, fileName, response);
    }


    public static void export(Map<String, Object> map, String template, String fileName, HttpServletResponse response) throws Exception {
        TemplateExportParams params = new TemplateExportParams(template, true);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        downLoadExcel(response, fileName, workbook);
    }

    /**
     * 动态表头导出
     *
     * @param colList   表头
     * @param list      数据
     * @param fileName  文件名
     * @param sheetName sheet名字
     * @return
     * @throws Exception
     */
    public static void exportDynamicHeader(List<ExcelExportEntity> colList, List<Map<String, Object>> list, String fileName, String sheetName, HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(sheetName, sheetName), colList, list);
        downLoadExcel(response, fileName, workbook);
    }

    public static void downLoadExcel(HttpServletResponse response, String fileName, Workbook workbook) throws IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.setForceFormulaRecalculation(true);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }


}
